package com.platform.license.model;

import de.schlichtherle.license.AbstractKeyStoreParam;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;

/**
 * Description: 自定义KeyStoreParam，用于将公私钥存储文件存放到其他磁盘位置而不是项目中
 *
 * @author libinal Created on 2019/9/22
 */
public class LicKeyStoreParam extends AbstractKeyStoreParam {

    private String storePath;
    private String alias;
    private String storePwd;
    private String keyPwd;

    /**
     * Creates a new instance of AbstractKeyStoreParam which will look up
     * the given resource using the class loader of the given class when
     * calling {@link #getStream()}.
     *
     * @param clazz    the class which refers to the class loader to use.
     * @param resource the resource to look up.
     */
    public LicKeyStoreParam(Class clazz, String resource, String alias, String storeKey, String keyPwd) {
        super(clazz, resource);
        this.storePath = resource;
        this.alias = alias;
        this.storePwd = storeKey;
        this.keyPwd = keyPwd;
    }

    @Override
    public String getAlias() {
        return alias;
    }

    @Override
    public String getStorePwd() {
        return storePwd;
    }

    @Override
    public String getKeyPwd() {
        return keyPwd;
    }

    /**
     * 复写 de.schlichtherle.license.AbstractKeyStoreParam的getStream()方法<br/>
     * 用于将公私钥存储文件存放到其他磁盘位置而不是项目中
     *
     * @return 文件流
     * @throws IOException 异常
     */
    @Override
    public InputStream getStream() throws IOException {
        return new ClassPathResource(storePath).getInputStream();
    }
}
